<template>
  <el-card>
    <div class="calendar-grid">
      <div class="calendar-cell" v-for="(item, i) in dataList" :key="i">
        <!-- 显示日期 -->
        <div class="day-header">
          <span class="day-number">{{ formatDay(item.dataDate) }}</span>
          <span class="weekday">{{ formatWeekday(item.dataDate) }}</span>
        </div>

        <!-- 滑块组件 -->
        <el-slider
          v-model="item.targetAmount"
          :min="0"
          :max="1000"
          :step="10"
          show-input
          input-size="large"
          class="day-slider"
          @change="handleSliderChange(item.dataDate)"
        >
          <template #suffix>
            <span class="slider-suffix">元</span>
          </template>
        </el-slider>
      </div>
    </div>
  </el-card>
</template>

<script>
export default {
  props: {
    dataList: {
      type: Array,
      required: true,
      validator: (arr) =>
        arr.every(
          (item) => item.dataDate && typeof item.targetAmount === "number"
        ),
    },
  },
  computed: {
    dateMap() {
      return this.dataList.reduce((acc, cur) => {
        acc[this.normalizeDate(cur.dataDate)] = cur;
        return acc;
      }, {});
    },
  },
  methods: {
    normalizeDate(dateStr) {
      return new Date(dateStr).toISOString().split("T")[0];
    },

    formatDay(dateStr) {
      return new Date(dateStr).getDate();
    },

    formatWeekday(dateStr) {
      const weekdays = ["日", "一", "二", "三", "四", "五", "六"];
      return weekdays[new Date(dateStr).getDay()];
    },

    handleSliderChange(date) {
      const normalized = this.normalizeDate(date);
      console.log(JSON.stringify(this.dateMap));
      if (this.dateMap[normalized]) {
        this.$emit("change", {
          date: normalized,
          amount: this.dateMap[normalized].targetAmount,
        });
      }
    },
  },
};
</script>

<style scoped>
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 16px;
  padding: 16px;
}

.calendar-cell {
  min-width: 140px;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  padding: 12px;
  background: #fff;
}

.day-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.day-number {
  font-size: 18px;
  font-weight: bold;
  color: #303133;
}

.weekday {
  font-size: 12px;
  color: #909399;
}

::v-deep .el-slider {
  display: flex;
  flex-direction: column;
}

::v-deep .el-slider__input {
  width: 200px !important;
  margin-top: 8px;
}

::v-deep .el-input__inner {
  padding-right: 25px !important;
}
::v-deep .el-slider__runway.show-input {
  margin-right: 160px;
  width: 1;
}
.slider-suffix {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 12px;
  color: #909399;
}

/* 响应式布局 */
@media (max-width: 1200px) {
  .calendar-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (max-width: 768px) {
  .calendar-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 480px) {
  .calendar-grid {
    grid-template-columns: 1fr;
  }
}
</style>